package com.example.demo.controller;

import com.example.demo.entity.Product.ProductDetail;
import com.example.demo.entity.Product.WorkArea;
import com.example.demo.services.ProductDetailService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

@RestController
@RequestMapping("/produceDetail")
public class ProductController {
    public final ProductDetailService service;

    public ProductController(ProductDetailService service) {
        this.service = service;
    }

    @PostMapping("/fetchDetail")
    public ProductDetail fetchDetail(@RequestBody Map<String, String> map) {
        String billNo = map.get("billNo");
        return service.fetchDetail(billNo);
    }

        @PostMapping("/addDetail")
    public boolean addDetail(@RequestBody Map<String, String> payload) {
        String billNo = payload.get("billNo");
        String billStatus = payload.get("billStatus");
        String billType = payload.get("billType");
        String workingAreaCode = payload.get("workingAreaCode");

        String inOut = payload.get("inOutDate");
        Instant instant = Instant.parse(inOut);
        ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String inOutDate = zdt.format(formatter);

        //LocalDateTime inOutDate = ZonedDateTime.parse(inOut).toLocalDateTime();
        String remark = payload.get("remark");
        String createBy = payload.get("createBy");
        String create = payload.get("createDate");
        LocalDateTime createDate = LocalDateTime.parse(create, DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"));
        System.out.println("inOutDate: " + inOutDate + "createBy: " + createBy + "createDate: " + createDate);

        ProductDetail productDetail = new ProductDetail();
        productDetail.setBillNo(billNo);
        productDetail.setBillStatus(billStatus);
        productDetail.setBillType(billType);
        productDetail.setWorkingAreaCode(workingAreaCode);
        productDetail.setInOutDate(inOutDate);
        productDetail.setRemark(remark);
        productDetail.setCreateBy(createBy);
        productDetail.setCreateDate(create);
        System.out.println(productDetail);
        return service.addDetail(productDetail);
    }

    @PostMapping("/getAreaName")
    public WorkArea getAreaName(@RequestBody Map<String, String> payload) {
        String workingAreaCode = payload.get("workingAreaCode");
        return service.getAreaName(workingAreaCode);
    }

    @PostMapping("/getBillNum")
    public String getBillNumber(@RequestBody Map<String, String> payload) {
        String billName = payload.get("billName");
        return service.getBillNumber(billName);
    }
}
